/***
 * * ASF - ACE Simulation Framework
 * * Copyright (C) 2007 Politecnico di Milano
 * * <mailto: daniel@dubois.it> <mailto: dinitto@elet.polimi.it>
 * *
 * * This library is free software; you can redistribute it and/or modify it
 * * under the terms of the GNU Lesser General Public License as published by
 * * the Free Software Foundation; either version 2.1 of the License, or (at
 * * your option) any later version.
 * *
 * * This library is distributed in the hope that it will be useful, but
 * * WITHOUT ANY WARRANTY; without even the implied warranty of
 * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * * General Public License for more details.
 * *
 * * You should have received a copy of the GNU Lesser General Public License
 * * along with this library; if not, write to the Free Software Foundation,
 * * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 ***/

package polimi.disttest;

import java.util.*;
import java.io.*;

/**
 * Class Results analyzer This class is an analyzer/writer for all the
 * statistical results obtained by the Manager.
 * 
 * @author Daniele Joseph Dubois
 */
public class ResultsAnalyzer {

	/** Statistical data */
	private Map<String, Object> data;

	/**
	 * This constructor creates a new ResultsAnalyzer for the given data.
	 * 
	 * @param data
	 *            Data to be analyzed.
	 */
	public ResultsAnalyzer(Map<String, Object> data) {
		this.data = data;
	}

	/**
	 * This method writes some of the results contained in this class to the
	 * specified file.
	 * 
	 * @param what
	 *            Name of the results to write
	 * @param fileName
	 *            Name of the filename to write
	 */
	public void writeResultsToFileCSV(List<String> what, String fileName) {
		String CSVfileName = fileName;
		FileWriter csv = null;
		String t = null;
		Object object = null;
		
		if (what==null || what.size()<=0) {
			what=new ArrayList<String>(data.keySet());
		}

		for (String s : what) {

			if (t == null) {
				t = "";
			} else {
				t += ",";
			}
			object = data.get(s);

			if (object instanceof String[]) {
				continue;
			}

			if (!((object instanceof Integer) || (object instanceof Double))) {
				t += "\"";
			}
			t += valueToString(object);
			if (!((object instanceof Integer) || (object instanceof Double))) {
				t += "\"";
			}
		}

		t += "\n";

		try {
			csv = new FileWriter(CSVfileName, true);
			csv.write(t);
			csv.flush();
			csv.close();
		} catch (FileNotFoundException e) {
			System.out.println("FATAL: unable to open file " + CSVfileName);
		} catch (IOException e) {
			System.out.println("FATAL: unable to write file " + CSVfileName);
		}
	}

	/**
	 * This method writes some of the results contained in this class to the
	 * specified file.
	 * 
	 * @param what
	 *            Name of the results to write
	 * @param fileName
	 *            Name of the filename to write
	 */
	public void writeResultsToFile(List<String> what, String fileName) {
		FileWriter file = null;
		String t = null;
		Object object = null;
		
		if (what==null || what.size()<=0) {
			what=new ArrayList<String>(data.keySet());
		}

		for (String s : what) {

			if (t == null) {
				t = s + "=";
			} else {
				t += "\n" + s + "=";
			}
			object = data.get(s);
			
			
			if (object instanceof String[]) {
				String[] ss = (String[]) object;
				t += "\"";
				for (String ss1:ss) {
					t+=ss1 + " ";
				}
				t += "\"";
			} else if (!((object instanceof Integer) || (object instanceof Double))) {
				t += "\"";
			}
			t += valueToString(object);
			if (!((object instanceof Integer) || (object instanceof Double))) {
				t += "\"";
			}
		}

		t += "\n";

		try {
			file = new FileWriter(fileName, false);
			file.write(t);
			file.flush();
			file.close();
		} catch (FileNotFoundException e) {
			System.err.println("FATAL: unable to open file " + fileName);
		} catch (IOException e) {
			System.err.println("FATAL: unable to write file " + fileName);
		}
	}

	
	/**
	 * This method prints some of the results contained in this class to the
	 * text console.
	 * 
	 * @param what
	 *            Name of the results to print
	 */
	public void writeResultsToConsole(List<String> what) {

		Integer maxLength = 0;
		String t;
		
		if (what==null || what.size()<=0) {
			what=new ArrayList<String>(data.keySet());
		}

		System.out.println("*** ALGORITHM RESULTS *** ");
		for (String s : what) {
			if ((s.length() > maxLength) && (valueToString(data.get(s)).length() > 0)) {
				maxLength = s.length();
			}
		}

		for (String s : what) {
			if (valueToString(data.get(s)).length() == 0) {
				continue; // hide parameters with no value
			}
			
			t = "";
			if (s.length() < maxLength) {
				for (int i = s.length(); i < maxLength; i++) {
					t += " ";
				}
			}
			t += s + " : " + valueToString(data.get(s));
			System.out.println(t);
		}
	}

	/**
	 * This method converts a generic object to a string suitable to be printed
	 * on the console or on a file.
	 * 
	 * @param value
	 *            Generic object to be converted
	 * @return String representation of the object
	 */
	private String valueToString(Object value) {

		if (value == null) {
			return "NULL";
		} else if (value instanceof String[]) {
			String s = "";
			for (int i = 0; i < ((String[]) value).length; i++) {
				s += "\n" + ((String[]) value)[i];
			}
			return s;
		} else if (value instanceof Boolean) {
			if ((Boolean) value) {
				return "TRUE";
			} else {
				return "FALSE";
			}
		}

		// use the standard java toString conversion here.
		return "" + value;
	}
}
